/**
 * Created by yangfan on 2016/11/1.
 */
define(['config/appregister',
    "i18n",
    "business/services/httpServices",
    "business/library/services/httpServices",
    "business/careerPlan/services/httpServices",
    "business/home/services/filterService",
    "business/services/components",
    "business/services/filter"], function (app, i18n, httpService,libHttpServices,carHttpservices) {
    app.controller("careerPlanDetailCtrl", function ($scope, DataServices,$state,$stateParams,auth) {
        $scope.gotoDetail=function(item){
            $state.go('eng.postbardeta',{postbarId:item.postbarId});
        }
        $scope.i18n = i18n;
        $scope.isAccessProcess = true ;
        $scope.grade1 = {
            course:"",
            file:"",
            skill:""
        };
        $scope.grade2 = {
            course:"",
            file:"",
            skill:""
        };
        $scope.pageNo = {
            grade1:{
                course:1,
                file:1,
                skill:1
            },
            grade2:{
                course:1,
                file:1,
                skill:1
            },
        };
        $scope.pageSize = 12;
        $scope.goTo =  function(state){
            auth.isLogin().then(function(){
                var arr = {
                    tcId:10,
                    hierId:$stateParams.hierId
                }
                var str = JSON.stringify(arr);
                sessionStorage.setItem('careerPostbar',str);
                $state.go(state)
            })

        };

        function getMoreArr(data){
            var arr = [];
            for (var i= 0 ; i <10; i ++){
               arr.push(data[0]);
            }
            return arr;
        }

        function setTableData(obj,type){

            if(!angular.isObject(obj)){
                return
            };
            if(!angular.isNumber(type)){
                return
            };
            var index = 0;
            var temp = [];
            temp[index]=[];
            for(var i=0;i<obj.length ; i++){
                temp[index].push(obj[i]);
                if((i+1)%type == 0&&i!=obj.length-1){
                    index++;
                    temp[index] = [];
                };
            }
            return temp;
        }

        function get(){
            var  req = {
                type:"GET",
                data:{
                    pageNo:1,
                    pageSize:10,
                    hierId:$stateParams.hierId
                },
                uri:"get"
            };
            carHttpservices.getPlannhier(req).success(function(response){
                $scope.$apply(function(){
                    $scope.model= response.data;
                })
            })
        }

        function getCourse(grade){
            var  req = {
                type:"GET",
                data:{
                    pageNo:1,
                    pageSize:$scope.pageSize,
                    hierId:$stateParams.hierId,
                    grade:grade
                },
                uri:"getCourse"
            };
            var obj = {
                grade:grade,
                key:"course",
                listNum:2,
                isSmallTable:true,
                title:"培训课程",
                logoSrc:"them/images/video_square.png",
                addMore:function(grade){
                    getCourse(grade)
                }
            };
           sendData (req,obj);
        }
        function sendData (req,obj){
            var grade = obj.grade;
            var gradeKey;
            if(grade == 1){
                gradeKey = "grade1"
            }else{
                gradeKey = "grade2"
            };
            req.data.pageNo = $scope.pageNo[gradeKey][obj.key];
            carHttpservices.getPlannhier(req).success(function(response){
                $scope.$apply(function(){
                    obj.response = response;
                    obj.totalPage = Math.floor(response.data.Pagination.totalCount / response.data.Pagination.pageSize);
                    resetData(obj);
                })
            })
        }

        function resetData(obj){

            //key like course1 or course2 file1 or file2
            var key = obj.key;
            var grade = obj.grade,gradeKey,response;
            if(grade == 1){
                gradeKey = "grade1"
            }else{
                gradeKey = "grade2"
            };
            response = obj.response;
            //判断分页
            if($scope.pageNo[gradeKey][key]==1){
                //$scope[gradeKey][key] =  getMoreArr(response.data.list);
                $scope[gradeKey][key] =  response.data.list;
            }else{
               // var arr =$scope[gradeKey][key].concat(getMoreArr(response.data.list));
                var arr =$scope[gradeKey][key].concat(response.data.list);
                $scope[gradeKey][key] = arr;
            }
            //生成conf 数据
            var conf = key+"Conf"+grade;
            var title = "title";
            if(obj.key =="file"){
                title = "name";
            }
            $scope[conf] = {
                grade:grade,
                key:obj.key,
                totalPage:obj.totalPage,
                pageNo:$scope.pageNo[gradeKey][key],
                data:setTableData($scope[gradeKey][key],obj.listNum),
                //data:$scope[gradeKey][key],
                isSmallTable:obj.isSmallTable,
                listNum:obj.listNum,
                listKey:title,
                logoSrc:obj.logoSrc,
                introduce:{
                    title:obj.title,
                    count:response.data.Pagination.totalCount + "个"
                },
                addMore:function(currentPage,grade){
                    $scope.pageNo[gradeKey][key] = currentPage;
                    obj.addMore(grade);
                }
            };
        }
        function getFile(grade){
            var  req = {
                type:"GET",
                data:{
                    pageNo:1,
                    pageSize:$scope.pageSize,
                    hierId:$stateParams.hierId,
                    grade:grade
                },
                uri:"getFile"
            };
            var obj = {
                grade:grade,
                key:"file",
                listNum:4,
                isSmallTable:false,
                title:"工程资料",
                addMore:function(grade){
                    getFile(grade)
                }
            };
            sendData (req,obj);
        }

        function getSkill(grade){
            var  req = {
                type:"GET",
                data:{
                    pageNo:1,
                    pageSize:$scope.pageSize,
                    hierId:$stateParams.hierId,
                    grade:grade
                },
                uri:"getSkill"
            };
            var obj = {
                grade:grade,
                key:"skill",
                listNum:2,
                isSmallTable:true,
                title:"工程技能",
                logoSrc:"them/images/ppt_logo.png",
                addMore:function(grade){
                    getSkill(grade)
                }
            };
            sendData (req,obj);
        }


        $scope.getList = {
            pageNo:1,
            pageSize:10
       	}
        $scope.getList2 = {
            pageNo:1,
            pageSize:10
        };
        function getList(grade,pageNo){
            var  req = {
                type:"GET",
                data:{
                    pageNo:pageNo,
                    pageSize:$scope.getList.pageSize,
                    heriId:$stateParams.heriId,
                    reward:'false',
                    grade:grade,
                    tcId:10,
                    time:'false',
                    replyCount:'false',
                    all:'false'
                },
                uri:"getList"
            };
            carHttpservices.getBostbar(req).success(function(response){
                if(grade ==1){
                    $scope.$apply(function(){
                        $scope.totalQuestion1 = response.data.Pagination.totalCount;
                        if($scope.getList.pageNo == 1){
                            $scope.list = response.data;
                            $scope.bostbarTitle = response.data.list[0];
                            $scope.bostbarList = response.data.list.slice(1);
                        }else{
                            var arr = [];
                            var temp = $scope.bostbarList;
                            if($scope.bostbarList &&$scope.bostbarList.length<1){
                                arr = response.data.list;
                            }else{
                                arr = temp.concat(response.data.list);
                            }
                            $scope.bostbarList = arr;
                        }
                    })
                }else if(grade ==2){
                    $scope.$apply(function(){
                        $scope.totalQuestion2 = response.data.Pagination.totalCount;
                        if($scope.getList2.pageNo == 1){
                            $scope.list2 = response.data;
                            $scope.bostbarTitle2 = response.data.list[0];
                            $scope.bostbarList2 = response.data.list.slice(1);
                        }else{
                            var arr = [];
                            var temp = $scope.bostbarList2;
                            if($scope.bostbarList2 &&$scope.bostbarList2.length<1){
                                arr = response.data.list;
                            }else{
                                arr = temp.concat(response.data.list);
                            }
                            $scope.bostbarList2 = arr;
                        }
                    })
                }
            })
        }

        $scope.loadMoreList = function(grade){
        	if(grade == 1){
        		$scope.getList.pageNo++;
            	getList(grade,$scope.getList.pageNo)
        	}else if(grade == 2){
        		$scope.getList2.pageNo++;
            	getList(grade,$scope.getList2.pageNo)
        	}
            
        };

        function init(){
            get();
            getCourse(1);
            getCourse(2);
            getFile(1);
            getFile(2);
            getSkill(1);
            getSkill(2);
            getList(1,1);
            getList(2,1);
        };
            init();
        $scope.goToAnchor =function(id){
            if(id =="anchor-rumen"){
                $scope.isAccessProcess =true ;
            }
            if(id =="anchor-jinjie"){
                $scope.isAccessProcess =false ;
            }
            $("html,body").animate({scrollTop: $("#"+id)[0].offsetTop - 135}, 500);
        };
		
		
		$scope.goBostbar = function(item){
			$state.go('eng.postbardeta',{postbarId:item.postbarId});
		}
    })
});

